<template>
  <el-dialog
    :title="!dataForm.id ? '新增' : '修改'"
    :close-on-click-modal="false"
    :visible.sync="visible"
  >
    <el-form ref="dataForm" :model="dataForm" :rules="dataRule" label-width="80px" @keyup.enter.native="dataFormSubmit()">
      <el-form-item label="分类名称" prop="catname">
        <el-input v-model="dataForm.catname" placeholder="" />
      </el-form-item>
      <el-form-item label="分类代码" prop="catcode">
        <el-input v-model="dataForm.catcode" placeholder="数据分类代码" />
      </el-form-item>
    </el-form>
    <span slot="footer" class="dialog-footer">
      <el-button size="mini" icon="el-icon-close" @click="visible = false">取消</el-button>
      <el-button size="mini" icon="el-icon-check" type="primary" @click="dataFormSubmit()">确定</el-button>
    </span>
  </el-dialog>
</template>

<script>
export default {
  data() {
    return {
      visible: false,
      dataForm: {
        catid: 0,
        catname: '',
        dataflag: '',
        catcode: ''
      },
      dataRule: {
        catname: [
          { required: true, message: '不能为空', trigger: 'blur' }
        ],
        dataflag: [
          { required: true, message: '数据有效标志1:有效 -1:无效不能为空', trigger: 'blur' }
        ],
        catcode: [
          { required: true, message: '数据分类代码不能为空', trigger: 'blur' }
        ]
      }
    }
  },
  methods: {
    init(id) {
      this.dataForm.catid = id || 0
      this.visible = true
      this.$nextTick(() => {
        this.$refs['dataForm'].resetFields()
        if (this.dataForm.catid) {
          this.$http({
            url: this.$http.adornUrl(`/data/info/${this.dataForm.catid}`),
            method: 'get',
            params: this.$http.adornParams()
          }).then(({ data }) => {
            if (data && data.code === 0) {
              this.dataForm.catname = data.dataCats.catname
              this.dataForm.dataflag = data.dataCats.dataflag
              this.dataForm.catcode = data.dataCats.catcode
            }
          })
        }
      })
    },
    // 表单提交
    dataFormSubmit() {
      this.$refs['dataForm'].validate((valid) => {
        if (valid) {
          this.$http({
            url: this.$http.adornUrl(`/data/${!this.dataForm.catid ? 'save' : 'update'}`),
            method: 'post',
            data: this.$http.adornData({
              'catid': this.dataForm.catid || undefined,
              'catname': this.dataForm.catname,
              'dataflag': this.dataForm.dataflag,
              'catcode': this.dataForm.catcode
            })
          }).then(({ data }) => {
            if (data && data.code === 0) {
              this.$message({
                message: '操作成功',
                type: 'success',
                duration: 1500,
                onClose: () => {
                  this.visible = false
                  this.$emit('refreshDataList')
                }
              })
            } else {
              this.$message.error(data.msg)
            }
          })
        }
      })
    }
  }
}
</script>
